﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shell;

namespace System
{
    public static class ViewExtension
    {
        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="element"></param>
        /// <param name="dependencyProperty"></param>
        /// <returns></returns>
        public static T GetValue<T>(this FrameworkElement element,DependencyProperty dependencyProperty)
        {
          return   (T)element.GetValue(dependencyProperty);
        }
        /// <summary>
        /// 批量往集合里面添加数据
        /// </summary>
        /// <typeparam name="TCollection"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="ts"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static TCollection AddRange<TCollection,T>(this TCollection ts,IEnumerable<T> data) where TCollection : ICollection<T>
        {
            foreach (var item in data)
            {
                ts.Add(item);
            }
            return ts;
        }
        /// <summary>
        /// 获取父窗口
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="userControl"></param>
        /// <returns></returns>
        public static TWindow GetWindow<TWindow>(this DependencyObject userControl) where TWindow:Window
        {
            var parent = LogicalTreeHelper.GetParent(userControl);
            if (parent != null)
            {
                if (parent is TWindow window)
                {
                    return window;
                }
                else
                {
                   return parent.GetWindow<TWindow>();
                }
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        ///将内容移到窗口中
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="dependencyObject"></param>
        /// <returns></returns>
        public static TWindow SetWindow<TWindow>(this DependencyObject dependencyObject) where TWindow : Window,new()
        {
            TWindow window = new TWindow();
            window.Content = dependencyObject;
            return window;  
        }
        /// <summary>
        /// 将内容移到窗口中
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <returns></returns>
        public static Window SetWindow(this DependencyObject dependencyObject)
        {
            return dependencyObject.SetWindow<Window>();
        }
        /// <summary>
        /// 设置宽度
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetWidth<TWindow>(this TWindow window, double value) where TWindow : Window
        {
            window.Width = value;
            return window;
        }
        /// <summary>
        /// 设置窗口宽度
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetWidthPercent<TWindow>(this TWindow window, double value) where TWindow : Window
        {
            var width = SystemParameters.WorkArea.Width;
            var height = SystemParameters.WorkArea.Height;
            window.Width = width * value;
            return window;
        }
        public static TWindow SetMaxWidthPercent<TWindow>(this TWindow window, double value) where TWindow : Window
        {
            var width = SystemParameters.WorkArea.Width;
            var height = SystemParameters.WorkArea.Height;
            window.MaxWidth = width * value;
            return window;
        }
        public static TWindow SetHeightPercent<TWindow>(this TWindow window, double value) where TWindow : Window
        {
            var height = SystemParameters.WorkArea.Height;
            window.Height = height * value;
            return window;
        }
        public static TWindow SetMaxHeightPercent<TWindow>(this TWindow window, double value) where TWindow : Window
        {
            var height = SystemParameters.WorkArea.Height;
            window.MaxHeight = height * value;
            return window;
        }
        /// <summary>
        /// 设置尺寸大小
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetSizePercent<TWindow>(this TWindow window, double value) where TWindow : Window
        {
            window.SetHeightPercent(value).SetWidthPercent(value);
            return window;
        }
        /// <summary>
        /// 设置最大尺寸百分比
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetMaxSizePercent<TWindow>(this TWindow window, double value) where TWindow : Window
        {
            window.SetMaxHeightPercent(value).SetMaxWidthPercent(value);
            return window;
        }
        /// <summary>
        /// 设置宽度
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetHeight<TWindow>(this TWindow window, double value) where TWindow : Window
        {
            window.Height = value;
            return window;
        }
        /// <summary>
        /// 设置窗口启动时显示的位置
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetWindowStartupLocation<TWindow>(this TWindow window, WindowStartupLocation value) where TWindow : Window
        {
            window.WindowStartupLocation = value;
            return window;
        }
        /// <summary>
        /// 设置窗口样式
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetWindowStyle<TWindow>(this TWindow window, WindowStyle value) where TWindow : Window
        {
            window.WindowStyle = value;
            return window;
        }
        /// <summary>
        /// 设置窗口是否置顶
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetTopmost<TWindow>(this TWindow window, bool value) where TWindow : Window
        {
            window.Topmost = value;
            return window;
        }
        /// <summary>
        /// 设置窗口状态
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetWindowState<TWindow>(this TWindow window, WindowState value) where TWindow : Window
        {
            window.WindowState = value;
            return window;
        }
        /// <summary>
        /// 设置标题
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetTitle<TWindow>(this TWindow window, string value) where TWindow : Window
        {
            window.Title = value;
            return window;
        }
        /// <summary>
        /// 设置尺寸模式
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetResizeMode<TWindow>(this TWindow window, ResizeMode value) where TWindow : Window
        {
            window.ResizeMode = value;
            return window;
        }
        /// <summary>
        /// 设置图标
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetIcon<TWindow>(this TWindow window, ImageSource value) where TWindow : Window
        {
            window.Icon = value;
            return window;
        }
        /// <summary>
        /// 设置是否显示在任务栏
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetShowInTaskbar<TWindow>(this TWindow window, bool value) where TWindow : Window
        {
            window.ShowInTaskbar = value;
            return window;
        }
        /// <summary>
        /// 设置任务栏提示工具
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetTaskbarItemInfo<TWindow>(this TWindow window, TaskbarItemInfo value) where TWindow : Window
        {
            window.TaskbarItemInfo = value;
            return window;
        }
        /// <summary>
        /// 设置尺寸随内容修改
        /// </summary>
        /// <typeparam name="TWindow"></typeparam>
        /// <param name="window"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TWindow SetSizeToContent<TWindow>(this TWindow window, SizeToContent value) where TWindow : Window
        {
            window.SizeToContent = value;
            return window;
        }
    }
}
